%function [W] = doHOGsvm(posHOGS, negHOGS);
%
function [W xMean] = doHOGsvm(posHOGS, negHOGS)

X = cat(2,posHOGS,negHOGS);
xMean = mean(X,2);
X = X - repmat(xMean, 1, size(X,2));
X(end+1,:) = 1;                      % augment vectors with a 1 so there is a bias term


Y = [ones(size(posHOGS,2),1); zeros(size(negHOGS,2),1)];
Y = int8(Y);

dataset = vl_maketrainingset(X,Y);
LAMBDA = .01;

% we have more negative than positive examples, so let's create a 
% "permutation or sampling matrix" that samples our few positive examples
%  more often.
nP = size(posHOGS,2);
nN = size(negHOGS,2);

ratio = 1+ floor(nN/(4*nP));


a = [repmat(1:nP, 1, ratio) nP+1:nP+nN];
b = randperm(length(a));   % i don't know if it needs to be randomized in order.
c = a(b);
c = uint32(c);

[W B INFO] = vl_svmpegasos(dataset, LAMBDA,...
    'MaxIterations', 100000,...
    'Permutation', c);
